function CollisionSolver(width,height){
	this.elasticForce = 1.0;
	
	this.screenWidth = width;
	this.screenHeight = height;
	
	this.resolveEdgesCollision = function(obj){
		if(obj.x+obj.collisionRadius>=this.screenWidth && obj.xVelocity>0){			
			obj.xVelocity = - obj.xVelocity; 
			obj.edgeCollisionCallback();
		}
		else if(obj.x-obj.collisionRadius<=0 && obj.xVelocity<0){			
			obj.xVelocity = - obj.xVelocity; 
			obj.edgeCollisionCallback();
		}
		if(obj.y+obj.collisionRadius>=this.screenHeight && obj.yVelocity>0){			
			obj.yVelocity = - obj.yVelocity; 
			obj.edgeCollisionCallback();
		}
		else if(obj.y-obj.collisionRadius<=0 && obj.yVelocity<0){			
			obj.yVelocity = - obj.yVelocity; 
			obj.edgeCollisionCallback();
		}
	}
	
	this.checkCollision = function(obj1, obj2){
		var xDistance = obj1.x - obj2.x;
		var yDistance = obj1.y - obj2.y;
		var radiusSum = obj1.collisionRadius + obj2.collisionRadius;
		
		return ((xDistance*xDistance + yDistance*yDistance)<=(radiusSum*radiusSum));	
	}	
	this.correctPosition = function (objp1,objp2,collisionAngle,dx,dy){
		var distance = Math.sqrt(dx*dx + dy*dy);
		var minDistance = objp1.collisionRadius + objp2.collisionRadius;
		var correctionDistance = minDistance - distance;		
		
		var correctionDistance1 = objp2.mass*correctionDistance/(objp1.mass + objp2.mass);
		var correctionDistance2 = objp1.mass*correctionDistance/(objp1.mass + objp2.mass);
		
		var x1cd = correctionDistance1*Math.cos(collisionAngle);
		var x2cd = -correctionDistance2*Math.cos(collisionAngle);
		
		var y1cd = correctionDistance1*Math.sin(collisionAngle);
		var y2cd = -correctionDistance2*Math.sin(collisionAngle);
		
		objp1.x+=x1cd;
		objp2.x+=x2cd;
		objp1.y+=y1cd;
		objp2.y+=y2cd;
	}
	this.solveCollision = function(obj1, obj2){	
		
		var objp1 = obj1.parent;
		var objp2 = obj2.parent;
		
		var dx = obj1.parent.x - obj2.parent.x;
		var dy = obj1.parent.y - obj2.parent.y;
		
		var collisionAngle = Math.atan2(dy,dx);
		
		this.correctPosition(objp1,objp2,collisionAngle,dx,dy);
		
		var speed1 = Math.sqrt(obj1.parent.xVelocity*obj1.parent.xVelocity + obj1.parent.yVelocity*obj1.parent.yVelocity);
		var speed2 = Math.sqrt(obj2.parent.xVelocity*obj2.parent.xVelocity + obj2.parent.yVelocity*obj2.parent.yVelocity);
				
		var direction1 = Math.atan2(obj1.parent.yVelocity,obj1.parent.xVelocity);
		var direction2 = Math.atan2(obj2.parent.yVelocity,obj2.parent.xVelocity);
		
		var velocityx_1 = speed1*Math.cos(direction1 - collisionAngle);
		var velocityy_1 = speed1*Math.sin(direction1 - collisionAngle);
		
		var velocityx_2 = speed2*Math.cos(direction2 - collisionAngle);
		var velocityy_2 = speed2*Math.sin(direction2 - collisionAngle);
		
		var final_velocityy_1 = velocityy_1;
		var final_velocityy_2 = velocityy_2;
		
		var final_velocityx_1 = this.elasticForce*((obj1.parent.mass - obj2.parent.mass)*velocityx_1 + (obj1.parent.mass + obj2.parent.mass)*velocityx_2)/(obj1.parent.mass + obj2.parent.mass);
		var final_velocityx_2 = this.elasticForce*((obj1.parent.mass + obj2.parent.mass)*velocityx_1 + (obj2.parent.mass - obj1.parent.mass)*velocityx_2)/(obj1.parent.mass + obj2.parent.mass);
		
		objp1.xVelocity = Math.cos(collisionAngle) * final_velocityx_1 + Math.cos(collisionAngle + Math.PI/2)*final_velocityy_1;
		objp1.yVelocity = Math.sin(collisionAngle) * final_velocityx_1 + Math.sin(collisionAngle + Math.PI/2)*final_velocityy_1;
		
		objp2.xVelocity = Math.cos(collisionAngle) * final_velocityx_2 + Math.cos(collisionAngle + Math.PI/2)*final_velocityy_2;
		objp2.yVelocity = Math.sin(collisionAngle) * final_velocityx_2 + Math.sin(collisionAngle + Math.PI/2)*final_velocityy_2;	

		objp2.objectCollisionCallback(obj2,obj1);
		objp1.objectCollisionCallback(obj1,obj2);
	}		
}
